
Imports std
Imports System.Text.RegularExpressions

Namespace htmlUtils

    Public Module htmlUtils

        Public Const linkRegex As String = "href=""(?<url>.*?)"""
        Public Const scriptStart As String = "<SCRIPT"
        'Public ReadOnly scriptStartLen As Int32 = strlen(scriptStart)
        Public Const scriptEnd As String = "</SCRIPT>"
        'Public ReadOnly scriptEndLen As Int32 = strlen(scriptEnd)
        Public Const styleStart As String = "<style"
        Public Const styleEnd As String = "</style>"
        Public Const remarkStart As String = "/*"
        Public Const remarkEnd As String = "*/"
        Public Const positionMark As String = network.linkPositionMark
        Public ReadOnly positionMarkLen As Int32 = strlen(positionMark)
        Public Const pathSeperator As String = network.pathSeperator
        Public ReadOnly pathSeperatorLen As Int32 = strlen(pathSeperator)

        Public Sub kickRemark(ByRef content As String)
            kickBetween(content, remarkStart, remarkEnd)
        End Sub

        Public Sub kickScript(ByRef content As String)
            'Dim i As Int32
            'Dim rtn As String = Nothing
            'Dim script As Int32 = 0
            'For i = 0 To strlen(content) - 1
            '    If strsame(content, i, scriptStart, 0, scriptStartLen, False) Then
            '        script += 1
            '        i += scriptStartLen
            '        i -= 1
            '    ElseIf strsame(content, i, scriptEnd, 0, scriptEndLen, False) Then
            '        script -= 1
            '        i += scriptEndLen
            '        i -= 1
            '    ElseIf script = 0 Then
            '        rtn += content(i)
            '    End If
            'Next

            'content = rtn
            kickBetween(content, scriptStart.ToUpper, scriptEnd.ToUpper)
            kickBetween(content, scriptStart.ToLower, scriptEnd.ToLower)
        End Sub

        Public Sub kickStyle(ByRef content As String)
            kickBetween(content, styleStart.ToUpper, styleEnd.ToUpper)
            kickBetween(content, styleStart.ToLower, styleEnd.ToLower)
        End Sub

        Public Sub kickTags(ByRef content As String)
            'Dim i As Int32
            'Dim bracket As Int32 = 0
            'Dim rtn As String = Nothing
            'For i = 0 To strlen(content) - 1
            '    Select Case content(i)
            '        Case character.leftAngleBracket
            '            bracket += 1
            '        Case character.rightAngleBracket
            '            bracket -= 1
            '        Case Else
            '            If bracket = 0 Then
            '                rtn += content(i)
            '            End If
            '    End Select
            'Next

            'content = rtn
            kickBetween(content, character.leftAngleBracket, character.rightAngleBracket)
        End Sub

        Public Sub kickAll(ByRef content As String)
            kickRemark(content)
            kickStyle(content)
            kickScript(content)
            kickTags(content)
        End Sub

        Public Sub alllinks(ByVal content As String, ByRef links As vector(Of String) _
                            , Optional ByVal pageurl As String = emptyString)
            Dim matches As MatchCollection = Nothing
            Dim match As Match = Nothing
            Dim regex As Regex = Nothing
            Dim emptyPageUrl As Boolean = isemptystring(pageurl)
            Dim pagepath As String = Nothing
            Dim rootpath As String = Nothing
            If links Is Nothing Then
                links = New vector(Of String)
            Else
                links.clear()
            End If

            If Not emptyPageUrl Then
                pagepath = urlUtils.parentpath(pageurl)
                rootpath = urlUtils.rootpath(pageurl)
            End If
            If Not isemptystring(content) Then
                regex = New Regex(linkRegex, RegexOptions.Compiled)
                matches = regex.Matches(content)
                For Each match In matches
#If Not PocketPC AndAlso Not Smartphone Then
                    Dim link As String = urlUtils.htmldecode(match.Groups.Item("url").ToString())
#Else
                    Dim link As String = match.Groups("url").ToString()
#End If
                    If Not strsame(link, positionMark, positionMarkLen, False) Then
                        If Not availableAddress(link) AndAlso Not emptyPageUrl Then
                            If strsame(link, pathSeperator, pathSeperatorLen, False) Then
                                link = rootpath + strmid(link, pathSeperatorLen)
                            Else
                                link = pagepath + link
                            End If
                        End If
                        If Not urlUtils.urlnormalize(link) Then
                            raiseError("cannot normalize url " + link, errorHandle.errorType.warning)
                        End If
                        links.push_back(link)
                    End If
                Next
            End If
        End Sub
    End Module

End Namespace